package com.controller;

import com.model.*;
import com.util.ConstantUtil;
import dubbo.PageDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/api")
@RestController
public class IndexController {

    @Autowired
    private PageDataService pageDataService;


    /**
     * 登录
     *
     * @param session
     * @param userPage
     * @return
     */
    @RequestMapping(value = "/loginSubmit", method = RequestMethod.POST)
    public ResponseMsg loginSubmit(HttpSession session, Users userPage) {
        ResponseMsg msgData = new ResponseMsg();
        msgData = pageDataService.loginSubmit(userPage);
        if (msgData.getStatus().equals("success")) {
            userPage = (Users) msgData.getData();
            session.setAttribute(ConstantUtil.loginSession, userPage);
        }
        return msgData;
    }

    /**
     * 获取游戏入口
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/loginGameSubmit", method = RequestMethod.POST)
    public ResponseMsg loginGameSubmit(HttpSession session) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ResponseMsg msgData = pageDataService.loginGameSubmit(userPage);
        if (msgData.getStatus().equals("success")) {
            Users userGamePage = (Users) msgData.getData();
            userPage.setHgGameLobbyUrl(userGamePage.getHgGameLobbyUrl());
            session.setAttribute(ConstantUtil.loginSession, userPage);
        }
        return msgData;
    }

    /**
     * 注册
     *
     * @param session
     * @param userName
     * @param lastName
     * @param firstName
     * @param birthday
     * @param password
     * @param repassword
     * @param sex
     * @param phone
     * @param email
     * @param verifyCode
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseMsg registerAPI(HttpSession session, @RequestParam("userName") String userName, @RequestParam("lastName") String lastName
            , @RequestParam("firstName") String firstName, @RequestParam("birthday") String birthday, @RequestParam("password") String password
            , @RequestParam("repassword") String repassword, @RequestParam("sex") Integer sex, @RequestParam("phone") String phone
            , @RequestParam("email") String email, @RequestParam("verifyCode") String verifyCode) {
        ResponseMsg msgData = new ResponseMsg();
        String kaptcha = (String) session.getAttribute(ConstantUtil.KAPTCHA_SESSION_KEY);
        if (!kaptcha.equals(verifyCode)) {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("验证码不正确！");
        } else if (!password.equals(repassword)) {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("两次密码不一致！");
        } else {
            Users userPage = new Users();
            userPage.setUserName(userName);
            userPage.setRealName(lastName + firstName);
            userPage.setPassword(password);
            userPage.setBirthday(birthday);
            userPage.setSex(sex);
            userPage.setPhone(phone);
            userPage.setEmail(email);
            msgData = pageDataService.registerByClient(userPage);
            if (msgData.getStatus().equals("success")) {
                msgData = pageDataService.loginSubmit(userPage);
                userPage = (Users) msgData.getData();
                session.setAttribute(ConstantUtil.loginSession, userPage);
            }
        }
        return msgData;
    }

    /**
     * 刷新用户信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/refreshUserInfo", method = RequestMethod.POST)
    public ResponseMsg refreshUserInfo(HttpSession session) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ResponseMsg msgData = new ResponseMsg();
        if (userPage != null) {
            msgData = pageDataService.refreshUserInfoByClient(userPage);
            if (msgData.getStatus().equals("success")) {
                userPage = (Users) msgData.getData();
                session.setAttribute(ConstantUtil.loginSession, userPage);
            }
        }
        return msgData;
    }

    /**
     * 忘记密码 - 发送手机验证码
     *
     * @param session
     * @param phone
     * @param userName
     * @param realName
     * @return
     */
    @RequestMapping(value = "/forgotSendPhoneCode", method = RequestMethod.POST)
    public ResponseMsg forgotSendPhoneCode(HttpSession session, @RequestParam("phone") String phone, @RequestParam("userName") String userName, @RequestParam("realName") String realName) {
        Users userPage1 = new Users();
        userPage1.setUserName(userName);
        userPage1.setRealName(realName);
        ResponseMsg msgData = pageDataService.refreshUserInfoByClient(userPage1);
        if (msgData.getStatus().equals("success")) {
            session.setAttribute(ConstantUtil.FORGOT_USER_SESSION, msgData.getData());
            Users userPage = new Users();
            userPage.setPhone(phone);

            String currtCode = ConstantUtil.createRandomVcode();
            userPage.setPhoneVerifyCode(currtCode);
            System.out.println(currtCode);
            session.setAttribute(ConstantUtil.MESSAGE_SESSION_CODE, currtCode);

            msgData = pageDataService.sendPhoneVerifyCodeByClient(userPage);
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("账户不存在！");
        }
        return msgData;
    }

    @RequestMapping(value = "/forgotSendEmailCode", method = RequestMethod.POST)
    public ResponseMsg forgotSendEmailCode(HttpSession session, @RequestParam("email") String email, @RequestParam("userName") String userName, @RequestParam("realName") String realName) {
        Users userPage1 = new Users();
        userPage1.setUserName(userName);
        userPage1.setRealName(realName);
        ResponseMsg msgData = pageDataService.refreshUserInfoByClient(userPage1);
        if (msgData.getStatus().equals("success")) {
            session.setAttribute(ConstantUtil.FORGOT_USER_SESSION, msgData.getData());
            Users userPage = new Users();
            userPage.setEmail(email);

            String currtCode = ConstantUtil.createRandomVcode();
            userPage.setEmailVerifyCode(currtCode);
            System.out.println(currtCode);
            session.setAttribute(ConstantUtil.EMAIL_SESSION_CODE, currtCode);

            msgData = pageDataService.sendEmailVerifyCodeByClient(userPage);
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("账户不存在！");
        }
        return msgData;
    }

    /**
     * 忘记密码 - 填完验证信息 下一步
     *
     * @param session
     * @param userName
     * @param realName
     * @param phoneNumber
     * @param verifyCode
     * @return
     */
    @RequestMapping(value = "/forgotFristStepByPhone", method = RequestMethod.POST)
    public ResponseMsg forgotFristStepByPhone(HttpSession session, @RequestParam("userName") String userName, @RequestParam("realName") String realName,
                                              @RequestParam("phone") String phoneNumber, @RequestParam("verifyCode") String verifyCode) {
        ResponseMsg msgData = new ResponseMsg();
        if (verifyCode.equals(session.getAttribute(ConstantUtil.MESSAGE_SESSION_CODE))) {
            msgData.setStatus(ResponseMsg.success);
            msgData.setData("/forgotpasswordtwo");
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("验证码不正确！");
        }
        return msgData;
    }

    @RequestMapping(value = "/forgotFristStepByEmail", method = RequestMethod.POST)
    public ResponseMsg forgotFristStepByEmail(HttpSession session, @RequestParam("userName") String userName, @RequestParam("realName") String realName,
                                              @RequestParam("email") String email, @RequestParam("verifyCode") String verifyCode) {
        ResponseMsg msgData = new ResponseMsg();
        if (verifyCode.equals(session.getAttribute(ConstantUtil.EMAIL_SESSION_CODE))) {
            msgData.setStatus(ResponseMsg.success);
            msgData.setData("/forgotpasswordtwo");
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("验证码不正确！");
        }
        return msgData;
    }

    /**
     * 忘记密码 - 重置密码
     *
     * @param session
     * @param newpassword
     * @return
     */
    @RequestMapping(value = "/reSetPassword", method = RequestMethod.POST)
    public ResponseMsg reSetPasswordApi(HttpSession session, @RequestParam("confirmpassword") String confirmpassword,
                                        @RequestParam("newpassword") String newpassword) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.FORGOT_USER_SESSION);
        ResponseMsg msgData = new ResponseMsg();
        if (userPage != null) {
            userPage.setNewPassword(newpassword);
            msgData = pageDataService.reSetPasswordByoldPassword(userPage);
            if (msgData.getStatus().equals("success")) {
                session.removeAttribute(ConstantUtil.FORGOT_USER_SESSION);
                msgData.setData("/forgotpasswordthree");
            }
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("设置密码过程失败！");
        }
        return msgData;
    }

    /**
     * 添加在线存款订单
     *
     * @param session
     * @param depositChannelId
     * @param depositMoney
     * @param bankCode
     * @return
     */
    @RequestMapping(value = "/addOnlineDepositOrder", method = RequestMethod.POST)
    public ResponseMsg addOnlineDepositOrder(HttpSession session, @RequestParam("depositChannelId") Integer depositChannelId,
                                             @RequestParam("depositMoney") Double depositMoney, @RequestParam("bankCode") String bankCode) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        DepositOrder depositOrderPage = new DepositOrder();
        depositOrderPage.setUserId(userPage.getUserId());
        depositOrderPage.setDepositChannelId(depositChannelId);
        depositOrderPage.setLastloginIp(userPage.getLastloginIp());
        depositOrderPage.setUserName(userPage.getUserName());
        depositOrderPage.setRequestDepositAmount(depositMoney);
        depositOrderPage.setBankCode(bankCode);
        ResponseMsg msgData = pageDataService.addOnlineDepositOrderByClient(depositOrderPage);
        /*if (msgData.getStatus().equals("success")) {
            DepositOrder orderPage = (DepositOrder) msgData.getData();
            DepositChannels channels = new DepositChannels();
            channels.setDepositChannelId(depositChannelId);
            channels.setRequestDepositAmount(String.valueOf(depositMoney));
            channels.setOrderNo(orderPage.getOrderNo());
            msgData = pageDataService.goPaymentPage(channels);
        }*/
        return msgData;
    }

    /**
     * 添加离线存款订单
     *
     * @param session
     * @param depositChannelId
     * @param depositMoney
     * @param depositBank
     * @param depositAccount
     * @param depositName
     * @param depositType
     * @return
     */
    @RequestMapping(value = "/addOfflineDepositOrder", method = RequestMethod.POST)
    public ResponseMsg addOfflineDepositOrder(HttpSession session, @RequestParam("depositChannelId") Integer depositChannelId,
                                              @RequestParam("depositMoney") Double depositMoney, @RequestParam("depositBank") String depositBank,
                                              @RequestParam("depositAccount") String depositAccount, @RequestParam("depositName") String depositName,
                                              @RequestParam("depositType") String depositType) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        DepositOrder depositOrderPage = new DepositOrder();
        depositOrderPage.setUserId(userPage.getUserId());
        depositOrderPage.setUserName(userPage.getUserName());
        depositOrderPage.setDepositChannelId(depositChannelId);
        depositOrderPage.setOfflineType(depositType);
        depositOrderPage.setLastloginIp(userPage.getLastloginIp());
        depositOrderPage.setUserBankAccountName(depositName);
        depositOrderPage.setUserBankName(depositBank);
        depositOrderPage.setUserBankNo(depositAccount);
        depositOrderPage.setRequestDepositAmount(depositMoney);
        ResponseMsg msgData = pageDataService.addOfflineDepositOrderByClient(depositOrderPage);
        return msgData;
    }


    /**
     * 提款申请
     *
     * @param session
     * @param bankId
     * @param withdrawalAmount
     * @return
     */
    @RequestMapping(value = "/withdrawalSubmit", method = RequestMethod.POST)
    public ResponseMsg withdrawalSubmit(HttpSession session, @RequestParam("bankId") Integer bankId, @RequestParam("withdrawalAmount") Double withdrawalAmount) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        Withdrawal withdrawalPage = new Withdrawal();
        withdrawalPage.setUserId(userPage.getUserId());
        withdrawalPage.setUserBankId(bankId);
        withdrawalPage.setRequestWithdrawalAmount(withdrawalAmount);
        withdrawalPage.setLastloginIp(userPage.getLastloginIp());
        ResponseMsg msgData = pageDataService.withdrawalSubmitByClient(withdrawalPage);
        return msgData;
    }

    /**
     * 获取存款方式支持的银行
     *
     * @param session
     * @param depositChannelId
     * @return
     */
    @RequestMapping(value = "/getChannelBanksById", method = RequestMethod.POST)
    public ResponseMsg getChannelBanksById(HttpSession session, @RequestParam("depositChannelId") Integer depositChannelId) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        DepositChannelBanks depositChannelBanksPage = new DepositChannelBanks();
        depositChannelBanksPage.setDepositChannelId(depositChannelId);
        ResponseMsg msgData = pageDataService.getChannelBanksById(depositChannelBanksPage);
        return msgData;
    }

    /**
     * 获取游戏场馆优惠
     *
     * @param session
     * @param gameType
     * @return
     */
    @RequestMapping(value = "/getCouponsOfGame", method = RequestMethod.POST)
    public ResponseMsg getCouponsOfGame(HttpSession session, @RequestParam("gameType") String gameType) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        GamePromotion gamePromotions = new GamePromotion();
        gamePromotions.setUserId(userPage.getUserId());
        //gamePromotions.setGameType(gameType);
        ResponseMsg msgData = pageDataService.getTransferPromotions(userPage.getUserId());
        return msgData;
    }

    /**
     * 转账申请
     *
     * @param session
     * @param fromGame
     * @param toGame
     * @param transferAmount
     * @return
     */
    @RequestMapping(value = "/transferSubmit", method = RequestMethod.POST)
    public ResponseMsg transferSubmit(HttpSession session, @RequestParam("sourceTransfer") String fromGame,
                                      @RequestParam("descTransfer") String toGame, @RequestParam("transferAmount") double transferAmount,
                                      @RequestParam("levelDiscounts") int levelDiscounts) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        Transfer transferPage = new Transfer();
        transferPage.setUserId(userPage.getUserId());
        transferPage.setFromGame(fromGame);
        transferPage.setToGame(toGame);
        transferPage.setTransferType("main".equalsIgnoreCase(toGame) ? 2 : 1);
        transferPage.setTransferAmount(transferAmount);
        transferPage.setPromotionRecordId(levelDiscounts);
        ResponseMsg msgData = pageDataService.transferByClient(transferPage);
        return msgData;
    }

    /**
     * 添加银行卡
     *
     * @param session
     * @param bankNo
     * @param userName
     * @param bankName
     * @param branchBankName
     * @return
     */
    @RequestMapping(value = "/addBank", method = RequestMethod.POST)
    public ResponseMsg addBank(HttpSession session, @RequestParam("bankNo") String bankNo,
                               @RequestParam("userName") String userName, @RequestParam("bankName") String bankName,
                               @RequestParam("branchBankName") String branchBankName) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        UserBank userBank = new UserBank();
        userBank.setUserId(userPage.getUserId());
        userBank.setUserBankName(bankName);
        userBank.setUserBankAccountName(userPage.getRealName());
        userBank.setUserBankNo(bankNo);
        userBank.setUserBranchBankName(branchBankName);
        ResponseMsg msgData = pageDataService.addBankByClient(userBank);
        return msgData;
    }

    /**
     * 删除银行卡
     *
     * @param session
     * @param bankId
     * @return
     */
    @RequestMapping(value = "/delBank", method = RequestMethod.POST)
    public ResponseMsg delBank(HttpSession session, @RequestParam("bankId") Integer bankId) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        UserBank userBank = new UserBank();
        userBank.setUserId(userPage.getUserId());
        userBank.setBankId(bankId);
        ResponseMsg msgData = pageDataService.deleteBankByClient(userBank);
        return msgData;
    }

    /**
     * 更改密码
     *
     * @param session
     * @param oldpassword
     * @param newpassword
     * @return
     */
    @RequestMapping(value = "/updateUserPassword", method = RequestMethod.POST)
    public ResponseMsg updateUserPassword(HttpSession session, @RequestParam("oldpassword") String oldpassword,
                                          @RequestParam("newpassword") String newpassword) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        userPage.setOldPassword(oldpassword);
        userPage.setNewPassword(newpassword);
        ResponseMsg msgData = pageDataService.updatePasswordByClient(userPage);
        return msgData;
    }

    /**
     * 更改账户信息
     *
     * @param session
     * @param userName
     * @param sex
     * @param userYear
     * @param userMonth
     * @param userDay
     * @param province
     * @param city
     * @param area
     * @param address
     * @return
     */
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public ResponseMsg updateUserInfo(HttpSession session, @RequestParam("userName") String userName,
                                      @RequestParam("sex") Integer sex, @RequestParam("userYear") String userYear,
                                      @RequestParam("userMonth") Integer userMonth, @RequestParam("userDay") Integer userDay,
                                      @RequestParam("province") String province, @RequestParam("city") String city,
                                      @RequestParam("area") String area, @RequestParam("address") String address) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        userPage.setRealName(userName);
        userPage.setBirthday(userYear + "-" + (userMonth > 9 ? userMonth : "0" + userMonth) + "-" + (userDay > 9 ? userDay : "0" + userDay));
        userPage.setSex(sex);
        userPage.setProvince(province);
        userPage.setCity(city);
        userPage.setArea(area);
        userPage.setAddressDetails(address);
        ResponseMsg msgData = pageDataService.updateUserInfoByClient(userPage);
        return msgData;
    }

    /**
     * 发送手机验证码
     *
     * @param session
     * @param phone
     * @return
     */
    @RequestMapping(value = "/sendPhoneCode", method = RequestMethod.POST)
    public ResponseMsg sendPhoneCode(HttpSession session, @RequestParam("phone") String phone) {
        Users sessionUserPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        Users userPage = new Users();
        userPage.setUserId(sessionUserPage.getUserId());
        userPage.setPhone(phone);

        String currtCode = ConstantUtil.createRandomVcode();
        userPage.setPhoneVerifyCode(currtCode);
        session.setAttribute(ConstantUtil.MESSAGE_SESSION_CODE, currtCode);

        ResponseMsg msgData = pageDataService.sendPhoneVerifyCodeByClient(userPage);
        return msgData;
    }

    /**
     * 账户绑定手机号码
     *
     * @param session
     * @param phone
     * @param verifyCode
     * @return
     */
    @RequestMapping(value = "/bindPhone", method = RequestMethod.POST)
    public ResponseMsg bindPhone(HttpSession session, @RequestParam("phone") String phone, @RequestParam("verifyCode") String verifyCode) {
        ResponseMsg msgData = new ResponseMsg();
        if (verifyCode.equals(session.getAttribute(ConstantUtil.MESSAGE_SESSION_CODE))) {
            Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
            userPage.setPhone(phone);
            msgData = pageDataService.bindingPhoneByClient(userPage);
            if (msgData.getStatus().equals("success")) {
                // 更新 session
                userPage.setPhone(phone);
                session.setAttribute(ConstantUtil.loginSession, userPage);
            }
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("验证码不正确！");
        }
        return msgData;
    }

    /**
     * 发送邮件验证码
     *
     * @param session
     * @param email
     * @return
     */
    @RequestMapping(value = "/sendEmailCode", method = RequestMethod.POST)
    public ResponseMsg sendEmailCode(HttpSession session, @RequestParam("email") String email) {
        Users sessionUserPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        Users userPage = new Users();
        userPage.setUserId(sessionUserPage.getUserId());
        userPage.setEmail(email);

        String currtCode = ConstantUtil.createRandomVcode();
        userPage.setEmailVerifyCode(currtCode);
        session.setAttribute(ConstantUtil.EMAIL_SESSION_CODE, currtCode);

        ResponseMsg msgData = pageDataService.sendEmailVerifyCodeByClient(userPage);
        return msgData;
    }

    /**
     * 账户绑定邮箱
     *
     * @param session
     * @param email
     * @param verifyCode
     * @return
     */
    @RequestMapping(value = "/bindEmail", method = RequestMethod.POST)
    public ResponseMsg bindEmail(HttpSession session, @RequestParam("email") String email, @RequestParam("verifyCode") String verifyCode) {
        ResponseMsg msgData = new ResponseMsg();
        if (verifyCode.equals(session.getAttribute(ConstantUtil.EMAIL_SESSION_CODE))) {
            Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
            userPage.setEmail(email);
            msgData = pageDataService.bindingEmailByClient(userPage);
            if (msgData.getStatus().equals("success")) {
                // 更新 session
                userPage.setEmail(email);
                session.setAttribute(ConstantUtil.loginSession, userPage);
            }
        } else {
            msgData.setStatus(ResponseMsg.failed);
            msgData.setData("验证码不正确！");
        }
        return msgData;
    }

    /**
     * 站内信
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/sysMessage", method = RequestMethod.POST)
    public ResponseMsg sysMessage(HttpSession session) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setUserId(userPage.getUserId());
        ResponseMsg msgData = pageDataService.systemMessageByClient(systemMessage);
        return msgData;
    }

    /**
     * 更新站内信状态(未读改已读)
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/updateMessageStatus", method = RequestMethod.POST)
    public ResponseMsg updateMessageStatus(HttpSession session, @RequestParam("messageId") int messageId) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        SystemMessageUser systemMessageUser = new SystemMessageUser();
        systemMessageUser.setId(messageId);
        systemMessageUser.setIsread(2);
        ResponseMsg msgData = pageDataService.updateSystemMessageByClient(systemMessageUser);
        return msgData;
    }

    public PageDataService getPageDataService() {
        return pageDataService;
    }

    public void setPageDataService(PageDataService pageDataService) {
        this.pageDataService = pageDataService;
    }


}
